import { mongo } from "../mogoConfig"

// 复合索引
const run = async () => {
    const tb = mongo.db("my_test").collection("inventory")

    await tb.dropIndexes()

    let result = await tb.createIndex({ ratings: 1 }, { name: "idx1" })
    console.log(JSON.stringify(result))

    let cursor = await tb.find({ ratings: [5, 9] })
    console.log("查询结果1---------------------------")
    for await (const item of cursor) {
        console.log(JSON.stringify(item))
    }

    cursor = await tb.find({ ratings: 5 })
    console.log("查询结果2---------------------------")
    for await (const item of cursor) {
        console.log(JSON.stringify(item))
    }


    cursor = await tb.find({ $or: [{ ratings: 9 }, { ratings: 8 }] })
    console.log("查询结果3---------------------------")
    for await (const item of cursor) {
        console.log(JSON.stringify(item))
    }


    cursor = await tb.find({ ratings: { $elemMatch: { $gt: 8 } } })
    console.log("查询结果4---------------------------")
    for await (const item of cursor) {
        console.log(JSON.stringify(item))
    }

}


run()
    .catch((error) => {
        console.log(error)
    })
    .finally(() => {
        mongo.close()
        console.log("执行结束")
    })